package com.binhthuan.catechist.client.data;

import com.binhthuan.catechist.shared.constant.Parameter;
import com.binhthuan.catechist.shared.constant.Service;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.widgets.form.fields.SelectItem;

public class CourseDS extends AbstractDS {

    private static CourseDS instance;

    private CourseDS() {
        super(Service.COURSE);

        DataSourceIntegerField id = new DataSourceIntegerField(Parameter.ID);
        id.setPrimaryKey(true);
        id.setHidden(true);

        DataSourceTextField code = new DataSourceTextField(Parameter.CODE);
        code.setRequired(true);

        SelectItem gradeEditor = new SelectItem(Parameter.GRADE);
        gradeEditor.setRequired(true);
        gradeEditor.setValueField(Parameter.ID);
        gradeEditor.setDisplayField(Parameter.NAME);
        gradeEditor.setOptionDataSource(GradeDS.getInstance());
        //gradeEditor.setDefaultToFirstOption(true);

        DataSourceTextField grade = new DataSourceTextField(Parameter.GRADE);
        grade.setValueXPath(combineXPath(Parameter.GRADE, Parameter.ID));
        grade.setTypeAsDataSource(GradeDS.getInstance());
        grade.setEditorType(gradeEditor);
        grade.setRequired(true);

        DataSourceTextField name = new DataSourceTextField(Parameter.NAME);
        name.setRequired(true);

        SelectItem shiftEditor = new SelectItem(Parameter.SHIFT);
        shiftEditor.setRequired(true);
        shiftEditor.setValueField(Parameter.ID);
        shiftEditor.setDisplayField(Parameter.NAME);
        shiftEditor.setOptionDataSource(ShiftDS.getInstance());
        //shiftEditor.setDefaultToFirstOption(true);

        DataSourceTextField shift = new DataSourceTextField(Parameter.SHIFT);
        shift.setValueXPath(combineXPath(Parameter.SHIFT, Parameter.ID));
        shift.setTypeAsDataSource(ShiftDS.getInstance());
        shift.setEditorType(shiftEditor);
        shift.setRequired(true);

        SelectItem venueEditor = new SelectItem(Parameter.VENUE);
        venueEditor.setRequired(true);
        venueEditor.setValueField(Parameter.ID);
        venueEditor.setDisplayField(Parameter.NAME);
        venueEditor.setOptionDataSource(VenueDS.getInstance());
        //venueEditor.setDefaultToFirstOption(true);

        DataSourceTextField venue = new DataSourceTextField(Parameter.VENUE);
        venue.setValueXPath(combineXPath(Parameter.VENUE, Parameter.ID));
        venue.setTypeAsDataSource(VenueDS.getInstance());
        venue.setEditorType(venueEditor);
        venue.setRequired(true);

        SelectItem yearEditor = new SelectItem(Parameter.YEAR);
        yearEditor.setRequired(true);
        yearEditor.setValueField(Parameter.ID);
        yearEditor.setDisplayField(Parameter.DESCRIPTION);
        yearEditor.setOptionDataSource(YearDS.getInstance());
        //yearEditor.setDefaultToFirstOption(true);

        DataSourceTextField year = new DataSourceTextField(Parameter.YEAR);
        year.setValueXPath(combineXPath(Parameter.YEAR, Parameter.ID));
        year.setTypeAsDataSource(YearDS.getInstance());
        year.setEditorType(yearEditor);
        year.setRequired(true);

        SelectItem trainerEditor = new SelectItem(Parameter.TRAINER);
        trainerEditor.setValueField(Parameter.ID);
        trainerEditor.setDisplayField(Parameter.FULL_NAME);
        trainerEditor.setOptionDataSource(TrainerDS.getInstance());

        DataSourceTextField trainers = new DataSourceTextField(Parameter.TRAINERS);
        trainers.setValueXPath(combineXPath(Parameter.TRAINERS, Parameter.ID));
        //trainers.setTypeAsDataSource(TrainerDS.getInstance());
        //trainers.setEditorType(trainerEditor);
        trainers.setMultiple(true);
        
        DataSourceTextField trainersFirstName = new DataSourceTextField(Parameter.FIRST_NAME);
        trainersFirstName.setValueXPath(combineXPath(Parameter.TRAINERS, Parameter.FIRST_NAME));
        trainersFirstName.setMultiple(true);

        DataSourceTextField trainersLastName = new DataSourceTextField(Parameter.LAST_NAME);
        trainersLastName.setValueXPath(combineXPath(Parameter.TRAINERS, Parameter.LAST_NAME));
        trainersLastName.setMultiple(true);

        setFields(id, code, grade, name, shift, venue, year, trainers, trainersFirstName, trainersLastName);
    }

    public static CourseDS getInstance() {
        if (instance == null) {
            instance = new CourseDS();
        }
        return instance;
    }
}
